package com.clayecloud.module_property_payment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_base.dialog.BaseDialog;
import com.clayecloud.lib_base.dialog.PayPasswordDialog;
import com.clayecloud.module_property_payment.adapter.PropertyPaymentListAdapter;
import com.clayecloud.module_property_payment.databinding.ActivityPropertyPaymentMainBinding;
import com.clayecloud.module_property_payment.entity.HouseOrder;
import com.clayecloud.module_property_payment.viewModels.PropertyPaymentViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Route(path = "/pay/index")
public class PropertyPaymentMainActivity extends AppCompatActivity implements ToastAction {

    private Context context = this;

    private ActivityPropertyPaymentMainBinding activityPropertyPaymentMainBinding;

    private PropertyPaymentViewModel propertyPaymentViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPropertyPaymentMainBinding  = DataBindingUtil.setContentView(this, R.layout.activity_property_payment_main);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        propertyPaymentViewModel = viewModelProvider.get(PropertyPaymentViewModel.class);

        // 初始化函数进行初始化
        init();
        // 绑定监听事件
        initListener();
    }


    /**
     * 初始化函数
     */
    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        activityPropertyPaymentMainBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        propertyPaymentViewModel.leadDate();
    }


    /**
     * 绑定监听事件
     */
    public void initListener(){
        activityPropertyPaymentMainBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });

        propertyPaymentViewModel.getHouseOrders().observe(this, new Observer<List<HouseOrder>>() {
            @Override
            public void onChanged(List<HouseOrder> houseOrders) {
                PropertyPaymentListAdapter propertyPaymentListAdapter = new PropertyPaymentListAdapter(context, houseOrders);
                activityPropertyPaymentMainBinding.recyclerView.setAdapter(propertyPaymentListAdapter);
                initAdapterListener(propertyPaymentListAdapter);
            }
        });

    }

    private void initAdapterListener(PropertyPaymentListAdapter propertyPaymentListAdapter){
        propertyPaymentListAdapter.setOnItemClickListener(new PropertyPaymentListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, HouseOrder item) {
                // 支付密码输入对话框
                new PayPasswordDialog.Builder(context)
                        .setTitle("请输入支付密码")
                        .setSubTitle(item.getOrderDescribe())
                        .setMoney("￥"+item.getOrderCost().setScale(2, BigDecimal.ROUND_HALF_DOWN))
                        .setListener(new PayPasswordDialog.OnListener() {
                            @Override
                            public void onCompleted(BaseDialog dialog, String password) {
                                toast(password);
                            }
                            @Override
                            public void onCancel(BaseDialog dialog) {
                            }
                        })
                        .show();
            }
        });
    }



    /**
     * 在删除此Activity时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }

}